#include "opreatons.h"
#include "ui_opreatons.h"
#include <QCloseEvent>

operations_dashboard::operations_dashboard(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::operations_dashboard)
    , ticketManagementWindow(nullptr)
    , orderProcessingWindow(nullptr)
    , customerServiceWindow(nullptr)
    , dataAnalyticsWindow(nullptr)
{
    ui->setupUi(this);
    setupConnections();
    updateDashboardData();
}

operations_dashboard::~operations_dashboard()
{
    closeAllSubWindows();
    delete ui;
}

void operations_dashboard::closeEvent(QCloseEvent *event)
{
    // 主窗口关闭时，关闭所有子窗口
    closeAllSubWindows();
    
    // 接受关闭事件
    event->accept();
}

void operations_dashboard::closeAllSubWindows()
{
    // 关闭所有子窗口
    if (ticketManagementWindow) {
        ticketManagementWindow->close();
        delete ticketManagementWindow;
        ticketManagementWindow = nullptr;
    }
    if (orderProcessingWindow) {
        orderProcessingWindow->close();
        delete orderProcessingWindow;
        orderProcessingWindow = nullptr;
    }
    if (customerServiceWindow) {
        customerServiceWindow->close();
        delete customerServiceWindow;
        customerServiceWindow = nullptr;
    }
    if (dataAnalyticsWindow) {
        dataAnalyticsWindow->close();
        delete dataAnalyticsWindow;
        dataAnalyticsWindow = nullptr;
    }
}

void operations_dashboard::setupConnections()
{
    connect(ui->ticketManagementButton, &QPushButton::clicked, 
            this, &operations_dashboard::onTicketManagementButtonClicked);
    connect(ui->orderProcessingButton, &QPushButton::clicked, 
            this, &operations_dashboard::onOrderProcessingButtonClicked);
    connect(ui->customerServiceButton, &QPushButton::clicked, 
            this, &operations_dashboard::onCustomerServiceButtonClicked);
    connect(ui->dataAnalyticsButton, &QPushButton::clicked, 
            this, &operations_dashboard::onDataAnalyticsButtonClicked);
}

void operations_dashboard::updateDashboardData()
{
    // 获取API实例
    Api* api = Api::getInstance();
    
    // 调用API获取仪表板统计数据
    api->getDashboardStatistics([this](bool success, const QJsonObject& response, const QString& errorMsg, int statusCode) {
        if (success && response["status"]=="ok") {
            // 解析统计数据
            QJsonObject data = response["data"].toObject();
            
            // 更新今日销售额
            ui->revenueValue->setText(QString("¥%1").arg(data["todayRevenue"].toString()));
            
            // 更新今日订单数
            ui->ordersValue->setText(data["todayOrders"].toString());
            
            // 更新活跃用户数
            ui->activeUsersValue->setText(data["activeUsers"].toString());
            
            // 更新待处理订单数
            ui->pendingOrdersValue->setText(data["pendingOrders"].toString());
            
            // 更新退票申请数
            ui->refundRequestsValue->setText(data["refundRequests"].toString());
            
            // 更新系统通知数
            ui->systemNotificationsValue->setText(data["systemNotifications"].toString());
            
            qDebug() << "仪表板数据更新成功";
        } else {
            // 如果API调用失败，使用默认数据
            qDebug() << "获取仪表板数据失败，使用默认数据:" << errorMsg;
            
            // 设置默认值
            ui->revenueValue->setText("¥12,880.00");
            ui->ordersValue->setText("1,288");
            ui->activeUsersValue->setText("2,456");
            ui->pendingOrdersValue->setText("45");
            ui->refundRequestsValue->setText("12");
            ui->systemNotificationsValue->setText("8");
        }
    });
}

void operations_dashboard::onSubWindowDestroyed(QObject *obj)
{
    // 当子窗口被销毁时，将对应的指针设为nullptr
    if (obj == ticketManagementWindow) {
        ticketManagementWindow = nullptr;
    } else if (obj == orderProcessingWindow) {
        orderProcessingWindow = nullptr;
    } else if (obj == customerServiceWindow) {
        customerServiceWindow = nullptr;
    } else if (obj == dataAnalyticsWindow) {
        dataAnalyticsWindow = nullptr;
    }
}

void operations_dashboard::onTicketManagementButtonClicked()
{
    if (!ticketManagementWindow) {
        ticketManagementWindow = new operations_management();
        
        // 设置窗口属性，确保子窗口独立存在
        ticketManagementWindow->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint | 
                                               Qt::WindowMinMaxButtonsHint | Qt::WindowSystemMenuHint);
        
        // 连接销毁信号，以便在窗口关闭时清理指针
        connect(ticketManagementWindow, &QObject::destroyed, this, &operations_dashboard::onSubWindowDestroyed);
    }
    
    // 显示票务管理界面
    ticketManagementWindow->show();
    ticketManagementWindow->raise();
    ticketManagementWindow->activateWindow();
}

void operations_dashboard::onOrderProcessingButtonClicked()
{
    if (!orderProcessingWindow) {
        orderProcessingWindow = new operations_order();
        
        // 设置窗口属性，确保子窗口独立存在
        orderProcessingWindow->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint | 
                                            Qt::WindowMinMaxButtonsHint | Qt::WindowSystemMenuHint);
        
        // 连接销毁信号，以便在窗口关闭时清理指针
        connect(orderProcessingWindow, &QObject::destroyed, this, &operations_dashboard::onSubWindowDestroyed);
    }
    
    // 显示订单处理界面
    orderProcessingWindow->show();
    orderProcessingWindow->raise();
    orderProcessingWindow->activateWindow();
}

void operations_dashboard::onCustomerServiceButtonClicked()
{
    if (!customerServiceWindow) {
        customerServiceWindow = new operations_user_service();
        
        // 设置窗口属性，确保子窗口独立存在
        customerServiceWindow->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint | 
                                             Qt::WindowMinMaxButtonsHint | Qt::WindowSystemMenuHint);
        
        // 连接销毁信号，以便在窗口关闭时清理指针
        connect(customerServiceWindow, &QObject::destroyed, this, &operations_dashboard::onSubWindowDestroyed);
    }
    
    // 显示客户服务界面
    customerServiceWindow->show();
    customerServiceWindow->raise();
    customerServiceWindow->activateWindow();
}

void operations_dashboard::onDataAnalyticsButtonClicked()
{
    if (!dataAnalyticsWindow) {
        dataAnalyticsWindow = new operations_data_statistics();
        
        // 设置窗口属性，确保子窗口独立存在
        dataAnalyticsWindow->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint | 
                                           Qt::WindowMinMaxButtonsHint | Qt::WindowSystemMenuHint);
        
        // 连接销毁信号，以便在窗口关闭时清理指针
        connect(dataAnalyticsWindow, &QObject::destroyed, this, &operations_dashboard::onSubWindowDestroyed);
    }
    
    // 显示数据分析界面
    dataAnalyticsWindow->show();
    dataAnalyticsWindow->raise();
    dataAnalyticsWindow->activateWindow();
}

